﻿using System;
using Cbs.Lucky.Events;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;

namespace Cbs.Lucky.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public MainWindowViewModel(
            RightFlyoutWithContentRegion rightFlyoutWithContentRegion,
            IEventAggregator eventAggregator)
        {
            _rightFlyoutWithContentRegion = rightFlyoutWithContentRegion;
            _eventAggregator = eventAggregator;
            _eventAggregator
                ?.GetEvent<ShowEvent>()
                ?.Subscribe(ShowEventAction);
            _eventAggregator
                ?.GetEvent<CloseEvent>()
                ?.Subscribe(CloseEventAction);
        }

        private void CloseEventAction(CloseEventDetail obj)
        {
            if (obj == null)
            {
                return;
            }
            switch (obj.TypeName)
            {
                case "CreateOrUpdate":
                    if (_rightFlyoutWithContentRegion == null)
                    {
                        return;
                    }
                    _rightFlyoutWithContentRegion.IsModal = false;
                    _rightFlyoutWithContentRegion.IsOpen = false;
                    break;
                default:
                    break;
            }
        }

        private void ShowEventAction(ShowEventDetail obj)
        {
            if (obj == null)
            {
                return;
            }
            switch (obj.TypeName)
            {
                case "CreateOrUpdate":
                    if (_rightFlyoutWithContentRegion == null)
                    {
                        return;
                    }
                    _rightFlyoutWithContentRegion.FlyoutHeader = obj.Title;
                    _rightFlyoutWithContentRegion.IsModal = true;
                    _rightFlyoutWithContentRegion.IsOpen = true;
                    _rightFlyoutWithContentRegion.FlyoutWidth = 300;
                    break;
                case "LoseQuery":
                    if (_rightFlyoutWithContentRegion == null)
                    {
                        return;
                    }
                    _rightFlyoutWithContentRegion.FlyoutHeader = obj.Title;
                    _rightFlyoutWithContentRegion.IsModal = true;
                    _rightFlyoutWithContentRegion.IsOpen = true;
                    _rightFlyoutWithContentRegion.FlyoutWidth = 400;
                    break;
                default:
                    break;
            }
        }

        private string _title = "";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private RightFlyoutWithContentRegion _rightFlyoutWithContentRegion;
        private IEventAggregator _eventAggregator;

        public RightFlyoutWithContentRegion RightFlyoutWithContentRegion
        {
            get { return _rightFlyoutWithContentRegion; }
            set { SetProperty(ref _rightFlyoutWithContentRegion, value); }
        }
    }

    public abstract class FlyoutWithContentRegionBase : BindableBase
    {
        private string _flyoutHeader;
        private bool _isOpen;
        private bool _isModal;

        /// <summary>
        /// 模态窗口
        /// </summary>
        public bool IsModal
        {
            get { return _isModal; }
            set { SetProperty(ref _isModal, value); }
        }

        /// <summary>
        /// 是否打开
        /// </summary>
        public bool IsOpen
        {
            get { return _isOpen; }
            set { SetProperty(ref _isOpen, value); }
        }

        /// <summary>
        /// 布局Header
        /// </summary>
        public string FlyoutHeader
        {
            get { return _flyoutHeader; }
            set
            {
                SetProperty(ref _flyoutHeader, value);
            }
        }
    }
    /// <summary>
    /// 右侧布局和内容
    /// </summary>
    public class RightFlyoutWithContentRegion : FlyoutWithContentRegionBase
    {
        private int _flyoutWidth = 300;

        /// <summary>
        /// 布局宽度
        /// </summary>
        public int FlyoutWidth
        {
            get { return _flyoutWidth; }
            set { SetProperty(ref _flyoutWidth, value); }
        }
    }

    /// <summary>
    /// 区域名枚举
    /// </summary>
    public enum RegionNameEnum
    {
        /// <summary>
        /// 主区域
        /// </summary>
        ContentRegion,
        /// <summary>
        /// 右侧布局区域
        /// </summary>
        RightFlyoutContentRegion
    }

    /// <summary>
    /// IRegionManager扩展
    /// </summary>
    public static class IRegionManagerExt
    {
        public static IRegionManager AddToRegion(this IRegionManager self, RegionNameEnum regionNameEnum, Type viewType)
        {
            return self?.AddToRegion(regionNameEnum.ToString(), viewType);
        }
        public static IRegionManager RegisterViewWithRegion(this IRegionManager self, RegionNameEnum regionNameEnum, Type viewType)
        {
            return self?.RegisterViewWithRegion(regionNameEnum.ToString(), viewType);
        }

        public static IRegionManager RegisterViewWithRegion(this IRegionManager self, RegionNameEnum regionNameEnum, Func<object> getContentDelegate)
        {
            return self?.RegisterViewWithRegion(regionNameEnum.ToString(), getContentDelegate);
        }
    }
}
